package com.repast.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.repast.common.Const;
import com.repast.common.ServerResponse;
import com.repast.dao.CallWaitressMapper;
import com.repast.dao.DeskTimeMapper;
import com.repast.dao.OrdersMapper;
import com.repast.pojo.CallWaitress;
import com.repast.pojo.DeskTime;
import com.repast.pojo.Orders;
import com.repast.service.ICallWaitressService;

@Service
public class CallWaitressServiceImpl implements ICallWaitressService {
	
	@Autowired
	private CallWaitressMapper callMapper;
	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private DeskTimeMapper deskTimeMapper;
	
	public ServerResponse callWaitress(String openid) throws Exception {
		List<Orders> list = ordersMapper.selectOrdersListByOpenidAndOrdersType(openid,Const.OrdesType.EAT_IN);
		for (Orders orders : list) {
			List<DeskTime> deskTimeList = deskTimeMapper.selectDeskTimeById(orders.getDeskId());
			for (DeskTime deskTime : deskTimeList) {
				String start = deskTime.getDeskTimeStartTime();
				String end = deskTime.getDeskTimeEndTime();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				if(start!=null&&end!=null){
					Date startTime = sdf.parse(start);
					Date endTime = sdf.parse(end);
					Date date = new Date();
					if(date.after(startTime) && date.before(endTime)){
						CallWaitress call = new CallWaitress();
						call.setCallStatus(Const.CallStatus.NO_RECEIVE);
						call.setOpenid(openid);
						call.setDeskId(orders.getDeskId());
						call.setCallCreateTime(sdf.format(new Date()));
						call.setCallUpdateTime(sdf.format(new Date()));
						int insertCall = callMapper.insert(call);
						if(insertCall!=0){
							return ServerResponse.createBySuccessMessage("呼叫成功,请稍候!");
						}
					}
				}
			}
		}
		return ServerResponse.createByErrorMessage("你当前没有任何堂食订单信息!");
	}

}
